/**
 * @see https://github.com/DaMouse404/koa-cache-control
 */

const HEADER_KEY = 'Cache-Control';

module.exports = function(defaults) {
    return function *cacheControl(next) {
        yield next;

        let options = this.cacheControl || defaults || {};
        let cacheControl = [];

        if(this.response.get(HEADER_KEY)) {
            return;
        }

        if(options.private) {
            cacheControl.push('private');
        }
        else if(options.public) {
            cacheControl.push('public');
        }

        if(options.noStore) {
            options.noCache = true;
            cacheControl.push('no-store');
        }

        if(options.noCache) {
            options.maxAge = 0;
            delete options.sMaxAge;
            cacheControl.push('no-cache');
        }

        if(options.noTransform) {
            cacheControl.push('no-transform');
        }

        if(options.proxyRevalidate) {
            cacheControl.push('proxy-revalidate');
        }

        if(options.mustRevalidate) {
            cacheControl.push('must-revalidate');
        }
        else if(!options.noCache) {
            if(options.staleIfError) {
                cacheControl.push(`stale-if-error=${options.staleIfError}`);
            }

            if(options.staleWhileRevalidate) {
                cacheControl.push(`stale-while-revalidate=${options.staleWhileRevalidate}`);
            }
        }

        if(Number.isInteger(options.maxAge)) {
            cacheControl.push(`max-age=${options.maxAge}`);
        }

        if(Number.isInteger(options.sMaxAge)) {
            cacheControl.push(`s-maxage=${options.sMaxAge}`);
        }

        if(cacheControl.length) {
            this.set(HEADER_KEY, cacheControl.join(','));
        }
    };
};
